package cn.topevery.garden.dal.dao.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.topevery.garden.dal.dao.ProjBasicInfoDao;
import cn.topevery.garden.entity.po.ProjBasicInfo;
import cn.topevery.garden.entity.query.ProjBasicInfoQuery;
import cn.topevery.garden.entity.vo.ProjBasicInfoVO;
import cn.topevery.garden.framework.base.BaseDaoImpl;
import hw.topevery.framework.db.entity.DataPager;
import hw.topevery.framework.db.entity.SqlQueryMate;
import hw.topevery.framework.db.enums.ScriptConditionEnum;
import hw.topevery.framework.entity.KeyValue;
import org.springframework.stereotype.Repository;

/**
 * @Author LiBo
 * @Date 2022/11/2 15:22
 */
@Repository
public class ProjBasicInfoDaoImpl extends BaseDaoImpl<ProjBasicInfo> implements ProjBasicInfoDao {

    @Override
    public DataPager<ProjBasicInfoVO> listPage(ProjBasicInfoQuery query) {
        Integer pageIndex = query.pageIndex - 1;
        String table = " t_proj_basic_info tpbi \n" +
                "left join t_proj_situation_record tpsr on tpbi.c_proj_id = tpsr.c_proj_id and tpsr.c_db_status = 0 \n" +
                "LEFT JOIN t_sys_dict tsd ON tsd.c_dict_id=tpbi.c_proj_state";
        String columns = "tpbi.*, tsd.c_dict_name projStateName";
        SqlQueryMate sqlQueryMate = new SqlQueryMate(table).setColumns(columns).setOrderBy("tpbi.c_id desc");
        sqlQueryMate.where(true, "tpbi.c_db_status", ScriptConditionEnum.Eq, 0)
                .where(ObjectUtil.isNotEmpty(query.getProjName()), "tpbi.c_proj_name", ScriptConditionEnum.Like, query.getProjName())
                .where(ObjectUtil.isNotEmpty(query.getBulidType()), "tpbi.c_bulid_type", ScriptConditionEnum.Eq, query.getBulidType())
                .where(ObjectUtil.isNotEmpty(query.getProjType()), "tpbi.c_proj_type", ScriptConditionEnum.Eq, query.getProjType())
                .where(ObjectUtil.isNotEmpty(query.getProjProp()), "tpbi.c_proj_prop", ScriptConditionEnum.Eq, query.getProjProp())
                .where(ObjectUtil.isNotEmpty(query.getProgramType()), "tpsr.c_program_type", ScriptConditionEnum.Eq, query.getProgramType())
                .where(ObjectUtil.isNotEmpty(query.getBuildDeptId()), "tpsr.c_build_dept_id", ScriptConditionEnum.Eq, query.getBuildDeptId())
                .where(ObjectUtil.isNotEmpty(query.getBuildDeptName()), "tpsr.c_build_dept_name", ScriptConditionEnum.Eq, query.getBuildDeptName())
                .where(ObjectUtil.isNotEmpty(query.getProjCategory()), "tpsr.c_proj_category", ScriptConditionEnum.Eq, query.getProjCategory())
                .where(ObjectUtil.isNotEmpty(query.getIsAssessmentTask()), "tpsr.c_is_assessment_task", ScriptConditionEnum.Eq, query.getIsAssessmentTask())
                .where(ObjectUtil.isNotEmpty(query.getIsAnnualPlan()), "tpsr.c_is_annual_plan", ScriptConditionEnum.Eq, query.getIsAnnualPlan())
                .where(ObjectUtil.isNotEmpty(query.getLandNature()), "tpsr.c_land_nature", ScriptConditionEnum.Eq, query.getLandNature())
                .where(ObjectUtil.isNotEmpty(query.getFundSourceCode()), "tpsr.c_fund_source_code", ScriptConditionEnum.Eq, query.getFundSourceCode())
                .where(ObjectUtil.isNotEmpty(query.getBuildNatureCode()), "tpsr.c_build_nature_code", ScriptConditionEnum.Eq, query.getBuildNatureCode())
                .where(ObjectUtil.isNotEmpty(query.getNatureStageCode()), "tpsr.c_nature_stage_code", ScriptConditionEnum.Eq, query.getNatureStageCode())
                .where(ObjectUtil.isNotEmpty(query.getProjStartDate()), "tpsr.c_proj_start_date", ScriptConditionEnum.Eq, query.getProjStartDate())
                .where(ObjectUtil.isNotEmpty(query.getProjCompleteDate()), "tpsr.c_proj_complete_date", ScriptConditionEnum.Eq, query.getProjCompleteDate())
                .where(ObjectUtil.isNotEmpty(query.getEstimateTotalMoney()), "tpsr.c_estimate_total_money", ScriptConditionEnum.Eq, query.getEstimateTotalMoney())
                .where(ObjectUtil.isNotEmpty(query.getYear()), "tpsr.c_year", ScriptConditionEnum.Eq, query.getYear())
                .where(ObjectUtil.isNotEmpty(query.getProjState()), "tpsr.c_proj_state", ScriptConditionEnum.Eq, query.getProjState())
                .where(ObjectUtil.isNotEmpty(query.getEmployName()), "tpsr.c_employ_name", ScriptConditionEnum.Eq, query.getEmployName())
                .where(ObjectUtil.isNotEmpty(query.getRepresentIds()), "tpsr.c_represent_ids", ScriptConditionEnum.Eq, query.getRepresentIds())
                .where(ObjectUtil.isNotEmpty(query.getRepresentNames()), "tpsr.c_represent_names", ScriptConditionEnum.Eq, query.getRepresentNames())
                .where(ObjectUtil.isNotEmpty(query.getBulidType()), "tpsr.c_bulid_type", ScriptConditionEnum.Eq, query.getBulidType())
                .where(ObjectUtil.isNotEmpty(query.getProjArea()), "tpsr.c_proj_area", ScriptConditionEnum.Eq, query.getProjArea())
                .where(ObjectUtil.isNotEmpty(query.getProjActualArea()), "tpsr.c_proj_actual_area", ScriptConditionEnum.Eq, query.getProjActualArea())
                .where(ObjectUtil.isNotEmpty(query.getProjAddress()), "tpsr.c_proj_address", ScriptConditionEnum.Like, query.getProjAddress())
                .where(ObjectUtil.isNotEmpty(query.getBulidContent()), "tpsr.c_bulid_content", ScriptConditionEnum.Like, query.getBulidContent())
                .where(ObjectUtil.isNotEmpty(query.getRemark()), "tpsr.c_remark", ScriptConditionEnum.Like, query.getRemark())
                .where(ObjectUtil.isNotEmpty(query.getOperator()), "tpsr.c_operator", ScriptConditionEnum.Like, query.getOperator())
                .where(ObjectUtil.isNotEmpty(query.getStreet()), "tpsr.c_street", ScriptConditionEnum.Like, query.getStreet())
        ;
        return getDataPager(sqlQueryMate, query.pageSize, pageIndex, ProjBasicInfoVO.class);
    }

    @Override
    public ProjBasicInfo getByProjId(String projId) {
        return getEntityByOneColumn(new KeyValue<>("c_proj_id", projId));
    }
}
